<?php

/**
 * 分包任务类
 */

namespace App\Jobs;

use App\Jobs\Job;
use Log;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use App\Repository\Main\GetOrderDataRepository;
use Carbon\Carbon;
use App\Repository\Stock\StockIsPayTwoRepository;

class GetYidongStatusOne extends Job
{
    protected $type;
    protected $orderNum;
    public $tries = 3; // 最多次数
    public $log = null;
    public $timeout = 3000;
    public function __construct($orderNum, $type, $time = 0)
    {
        $this->log = new Logger('GetYidongData');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/GetYidongData.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
        $this->type = $type;
        $this->orderNum = $orderNum;
        $this->delay($time);  // 多少s后执行
    }

    /**
     * 执行任务
     *
     * @return void
     */
    public function handle()
    {
        // sleep(10);
        // echo $this->orderNum;
        // echo $this->type;
        ini_set('max_execution_time', 300);
        $model = new StockIsPayTwoRepository();
        $where = [
            'orderno' => $this->orderNum,
        ];
        if (is_array($this->orderNum)) {
            $where = $this->orderNum;
        }

        $res = $model->index($where);
        $this->log->info('提交上游失败操作' . $this->type . '参数' . (is_array($this->orderNum) ? json_encode($this->orderNum) : $this->orderNum));
        // Log::info('拆包操作222---' . $this->id);
        // $this->log->info('提交上游操作');
        // $modelTasking = new WlktaskingRepository();
        // $modelTasking->changeInfo($this->id);
        //$this->job->delete();
    }
    public function retryUntil()
    {
        //任务超时时间
        $time = 24 * 60 * 60;
        return Carbon::now()->addSeconds($time);
    }

    /**
     * 处理失败任务
     *
     * @return void
     */
    public function failed()
    {
        $this->log->info('提交上游失败操作' . $this->type . '参数' . (is_array($this->orderNum) ? json_encode($this->orderNum) : $this->orderNum));
        // $time = 60 * 4;
        // $where = [
        //     'orderno' => $this->orderNum,
        // ];
        // // 延时多长时间 延长4个小时在重新提交
        // if (is_array($this->orderNum)) {

        //     $where = $this->orderNum;
        // }
        // echo 44444444;
        // dispatch(new OrderSubmit($where, $this->type))->delay(Carbon::now()->addMinutes($time));
        // $modelTasking = new WlktaskingRepository();
        // $modelTasking->restore($this->id);
    }
}
